#!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash EIG-7.2.1.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* || ! -d "$1" || "$1" = "/" ]] &&  echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* || ! -d "$2" || "$2" = "/" ]] &&  echo "请输入正确的安装路径" && exit 1

buildpath=$1
installpath=$2
[[ "$buildpath" =~ .*/$ ]] && buildpath=${buildpath%/*}
[[ "$installpath" =~ .*/$ ]] && installpath=${installpath%/*}
[[ "$buildpath" == "$installpath" ]] && echo "构建路径与安装路径需为不同路径" && exit 1
yum install -y wget tar environment-modules gsl gsl-devel openblas openblas-devel 
set -e
wget -t 0 -c -P $buildpath https://github.com/DReichLab/EIG/archive/v7.2.1.tar.gz
tar xf $buildpath/v7.2.1.tar.gz -C $buildpath && rm -rf $buildpath/v7.2.1.tar.gz
cd $buildpath/EIG-7.2.1/src
export CC=clang
export CXX=clang++
export FC=flang
sed -i '12i override CFLAGS += -I/usr/include' Makefile
sed -i '13i override LDFLAGS += -L/usr/lib64' Makefile
make
make install
mkdir -p $installpath/EIG-7.2.1 && cp -r $buildpath/EIG-7.2.1/* $installpath/EIG-7.2.1
cat>"$installpath/EIG-7.2.1/EIG_modulefiles"<<EOF
#%Module1.0
conflict eigensoft
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
    set modfile_path [file dirname \$modfile]
    return \$modfile_path
}
set pwd [getModulefileDir]
set EIG \$pwd
setenv EIG \$EIG
prepend-path PATH \$EIG/bin
EOF

rm -rf $buildpath/EIG-7.2.1
